package com.intelligence.tio.server;

import java.util.HashMap;
import java.util.Map;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioAbsAioHandler;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.server.handler.*;

import com.intelligence.tio.server.utils.MessyCodeCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.json.Json;
import org.tio.server.intf.ServerAioHandler;

/**
* @Title: TioServerAioHandler.java 
* @Package com.intelligence.tio.server 
* @Description: Tio消息处理器分发类，不同业务逻辑调用不用的处理类。
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午2:46:48 
* @version V1.0
 */
public class TioServerAioHandler extends TioAbsAioHandler implements ServerAioHandler<TioSessionContext, JsonPacket, Object>
{
	private static Logger log = LoggerFactory.getLogger(TioServerAioHandler.class);

	private static Map<Integer, AbsTioBsHandler<?>> handlerMap = new HashMap<>();
	
	static
	{
		handlerMap.put(Command.COMMAND_SEND_GROUP_MSG_REQ.getValue(), new GroupMsgReqHandler());
		handlerMap.put(Command.COMMAND_HANDSHAKE_REQ.getValue(), new HeartbeatReqHandler());
		handlerMap.put(Command.COMMAND_JOIN_GROUP_REQ.getValue(), new JoinGroupReqHandler());
		handlerMap.put(Command.COMMAND_AUTH_REQ.getValue(), new AuthReqHandler());
		handlerMap.put(Command.COMMAND_CHAT_REQ.getValue(), new P2PReqHandler());
		handlerMap.put(Command.COMMAND_ONLINEUSERLIST_REQ.getValue(), new OnlineUserListReqHandler());
		handlerMap.put(Command.COMMAND_USERTOONLINE_REQ.getValue(),  new UserToOnlineReqHandler());
		handlerMap.put(Command.COMMAND_HEARTBEAT_REQ.getValue(),  new HeartbeatReqHandler());
		handlerMap.put(Command.COMMAND_OUTLINE_REQ.getValue(),  new UserExitReqHandler());
		handlerMap.put(Command.COMMAND_KICKUSER_REQ.getValue(),  new KickUserReqHandler());
	}
	
	
	/** 
	 * 处理消息
	 */
	@Override
	public Object handler(JsonPacket packet, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception{
		
		
		//将字节转成字符串
		byte[] body = packet.getBody();
		String data = new String(body,packet.CHARSET);
		//判断是否是flash默认发送的请求 data="flex来了"
		//如果是则直接return null
		if(data.contains("Flex来了")){
			log.info("拒绝flex默认请求");
			return null;
		}

		AbsTioBsHandler<?> showcaseBsHandler = null;
		Integer command = null;
		//判断是否是心跳包乱码
//		if(MessyCodeCheck.isMessyCode("�")){
//			showcaseBsHandler = handlerMap.get(1);
//			command = 1;
//			log.error("心跳包检测.");
//			return null;
//		}
		try {
			Map<String, Object> bean = Json.toBean(data, Map.class);
			//获取命令类型
			command = (Integer) bean.get("command");
			showcaseBsHandler = handlerMap.get(command);
		}catch(Exception e){
			//调用数据异常对象处理
			showcaseBsHandler = new DataErrorReqHandler();
			command = Command.COMMAND_DATA_ERROR_RESP.getValue();
		}

		if (showcaseBsHandler == null)
		{
			log.error("{}, 找不到处理类，type:{}", channelContext, command);
			return null;
		}
		showcaseBsHandler.handler(packet, channelContext);
		return null;
	}
}
